package graphics;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.SQLException;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;

import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import core.CustomerManagement;
import core.Message;
import core.ProductManagement;
import db.DBConnection;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NuovoProdottoJPanel extends javax.swing.JPanel {
	private JLabel iconjLabel;
	private JLabel codicejLabel;
	private JTextField prezzoSaccojTextField;
	private JLabel prezzoAlBancalejLabel;
	private JLabel prezzoAlSaccojLabel;
	private JLabel nomejLabel;
	private JTextField prezzoBancalejTextField;
	private JTextField nomejTextField;
	private JTextField codicejTextField;
	private JButton salvajButton;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new NuovoProdottoJPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public NuovoProdottoJPanel() {
		super();
		initGUI();
		this.aggiungiKeyListener();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(587, 234));
			{
				salvajButton = new JButton();
				salvajButton.setText("Salva (F12)");
				salvajButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/save-icon32.png")));
				// salvajButton.setMnemonic(KeyEvent.VK_F12);
				salvajButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						salvaCliente();
					}
				});
				
			}
			{
				iconjLabel = new JLabel();
				iconjLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/product-icon.png")));
			}
			{
				codicejLabel = new JLabel();
				codicejLabel.setText("Codice");
			}
			{
				codicejTextField = new JTextField();
				codicejTextField.setEditable(false);
				codicejTextField.setEnabled(false);

			}
			{
				nomejTextField = new JTextField();
				nomejTextField.addKeyListener(new KeyListener() {
					
					@Override
					public void keyTyped(KeyEvent arg0) {
						// TODO Auto-generated method stub
					}
					
					@Override
					public void keyReleased(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void keyPressed(KeyEvent arg0) {
						// TODO Auto-generated method stub
						int tasto = arg0.getKeyCode();
						if(tasto == KeyEvent.VK_F12)
						{
							salvaCliente();
						}
						else if(tasto == KeyEvent.VK_ENTER)
						{
							nomejTextField.transferFocus();
						}
					}
				});
			}
			{
				prezzoSaccojTextField = new JTextField();
				prezzoSaccojTextField.addKeyListener(new KeyListener() {
					
					@Override
					public void keyTyped(KeyEvent arg0) {
						// TODO Auto-generated method stub
					}
					
					@Override
					public void keyReleased(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void keyPressed(KeyEvent arg0) {
						// TODO Auto-generated method stub
						int tasto = arg0.getKeyCode();
						if(tasto == KeyEvent.VK_F12)
						{
							salvaCliente();
						}
						else if(tasto == KeyEvent.VK_ENTER)
						{
							prezzoSaccojTextField.transferFocus();
						}
					}
				});
			}
			{
				prezzoBancalejTextField = new JTextField();
				prezzoBancalejTextField.addKeyListener(new KeyListener() {
					
					@Override
					public void keyTyped(KeyEvent arg0) {
						// TODO Auto-generated method stub
					}
					
					@Override
					public void keyReleased(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void keyPressed(KeyEvent arg0) {
						// TODO Auto-generated method stub
						int tasto = arg0.getKeyCode();
						if(tasto == KeyEvent.VK_F12)
						{
							salvaCliente();
						}
						else if(tasto == KeyEvent.VK_ENTER)
						{
							prezzoBancalejTextField.transferFocus();
						}
					}
				});
			}
			{
				nomejLabel = new JLabel();
				nomejLabel.setText("Nome");
			}
			{
				prezzoAlSaccojLabel = new JLabel();
				prezzoAlSaccojLabel.setText("Prezzo al sacco");
			}
			{
				prezzoAlBancalejLabel = new JLabel();
				prezzoAlBancalejLabel.setText("Prezzo al Bancale");
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(24, 24)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGap(0, 0, Short.MAX_VALUE)
				        .addComponent(iconjLabel, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(16)
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(codicejTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				            .addComponent(codicejLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(nomejTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				            .addComponent(nomejLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(prezzoSaccojTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				            .addComponent(prezzoAlSaccojLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(prezzoBancalejTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
				            .addComponent(prezzoAlBancalejLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				        .addGap(18)
				        .addComponent(salvajButton, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 10, Short.MAX_VALUE)))
				.addContainerGap(195, 195));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(salvajButton, 0, 189, Short.MAX_VALUE)
				        .addGap(130))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addPreferredGap(salvajButton, prezzoAlBancalejLabel, LayoutStyle.ComponentPlacement.INDENT)
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(prezzoAlBancalejLabel, GroupLayout.Alignment.LEADING, 0, 104, Short.MAX_VALUE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(prezzoAlSaccojLabel, 0, 94, Short.MAX_VALUE)
				                .addGap(10))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(nomejLabel, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 63, Short.MAX_VALUE))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(codicejLabel, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 49, Short.MAX_VALUE)))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(prezzoBancalejTextField, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
				                .addGap(132))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(prezzoSaccojTextField, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
				                .addGap(131))
				            .addComponent(nomejTextField, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 203, GroupLayout.PREFERRED_SIZE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(codicejTextField, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
				                .addGap(132)))))
				.addGap(69)
				.addComponent(iconjLabel, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(51, 51));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void aggiungiKeyListener()
	{
		this.requestFocusInWindow();
		this.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				int tasto = arg0.getKeyCode();
				if(arg0.getKeyCode() == KeyEvent.VK_F12)
				{
					salvaCliente();
				}
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	private void salvaCliente()
	{
		System.out.println("salvo");
		Message mes = new Message(new JFrame());
		if(nomejTextField.getText().isEmpty())
		{
			mes.errorMessage("Mancano dati per effettuare l'inserimento", "Errore inserimento");
		}
		else
		{
			String [] parameters = new String[] {"", "", "",};
			parameters[0] = nomejTextField.getText();
			parameters[1] = prezzoSaccojTextField.getText().replace(',', '.');
			parameters[2] = prezzoBancalejTextField.getText().replace(',', '.');
			
			
			try {
				ProductManagement.insertProduct(new DBConnection(), parameters);
				mes.info("Inserimento effettuato correttamente");
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				mes.errorMessage("Errore durante l'inserimento", "Errore inserimento");
			}
		}
	}
}
